﻿using Org.BouncyCastle.Asn1.Cms;
using Repository.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Repository.Dtos {
	public static class DtoExtensions {
		public static Survey ToSurvey(this SurveyCreateDto dto) {
			return new Survey() {
				Id = Guid.NewGuid(),
				Name = dto.Name,
				Description = dto.Description,
				Reward = dto.Reward,
				State = dto.State,
				CreatedTime = DateTime.Now,
			};
		}
		public static Choice ToChoice(this ChoiceDto dto) {
			return new Choice() {
				Id = Guid.NewGuid(),
				Index = dto.Index,
				Description = dto.Description,
			};
		}

		public static Question ToQuestion(this QuestionDto dto) {
			return new Question() {
				Id = Guid.NewGuid(),
				Type = dto.Type,
				//Index = dto.Index,
				Description = dto.Content,
			};
		}


		public static SurveyCreateDto ToCreateDto(this FrontEndSurveyDto fdto) {
			return new SurveyCreateDto() {
				Name = fdto.Title,
				Description = "无问卷描述",
				Reward = 50,
				State = EnumSurveyState.Running,
				UserId = fdto.UserId,
			};
		}

		public static List<QuestionDto> ToQuestionDto(List<FrontEndQuestionDto> fdto) {
			var ret = new List<QuestionDto>();

			for (int i = 0; i < fdto.Count; i++) {
				var qdto = new QuestionDto() {
					Type = fdto[i].QuesitonType,
					Index = i,
					Content = fdto[i].Content,
					Options = new List<ChoiceDto>()
				};
				if (fdto[i].QuesitonType == EnumQuesitonType.SingleChoice)
					for (int j = 0; j < fdto[i].Options.Count; j++) {
						var cdto = new ChoiceDto() {
							Index = j,
							Description = fdto[i].Options[j].Value,
						};
						qdto.Options.Add(cdto);
					}
				ret.Add(qdto);
			}
			return ret;
		}

	}
}
